springboot拦截器HandlerInterceptor的注入失效问题 |
您所在的位置:网站首页 › springboot interceptor注入 › springboot拦截器HandlerInterceptor的注入失效问题 |
https://www.zhangshengrong.com/p/281oq7kbNw/ 一、问题描述项目启动后,拦截器下的注入为null,影响拦截器的逻辑操作。如下图: 拦截器加载是在springcontext创建之前完成,详情可以看spring的拦截器加载过程及IOC的关系 解决方案一:使用@Bean在拦截器初始化之前让类加载,重点在于@Bean的拦截器处理及getCheckFilter()的引入 具体如下: // 拦截器处理 public class CheckFilter implements HandlerInterceptor { @Autowired private SystemService systemService; } // 配置类处理 @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { @Bean public CheckFilter getCheckFilter(){ return new CheckFilter(); } @Override protected void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(getCheckFilter()).addPathPatterns("/**"). excludePathPatterns("/admin/auth/**"); super.addInterceptors(registry); } @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); super.addResourceHandlers(registry); } @Override protected void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/WEB-INF/",".jsp"); super.configureViewResolvers(registry); } } } 解决方案二:给拦截器增加配置注解如:@Configuration 、@Component 。在配置类中注入,具体如下操作: // 拦截器处理 @Configuration //或者 @Component public class CheckFilter implements HandlerInterceptor { @Autowired private SystemService systemService; } // 配置类处理 @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { @Autowired private CheckFilter checkFilter; @Override protected void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(checkFilter).addPathPatterns("/**"). excludePathPatterns("/admin/auth/**"); super.addInterceptors(registry); } @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); super.addResourceHandlers(registry); } @Override protected void configureViewResolvers(ViewResolverRegistry registry) { registry.jsp("/WEB-INF/",".jsp"); super.configureViewResolvers(registry); } } } 三、知识总结 1、涉及spring的拦截器加载过程及IOC的管理 2、参考资料//www.jb51.net/article/223241.htm 3、问题归纳 springboot HandlerInterceptor 拦截器匹配规则写错 导致 拦截器无效项目中发现拦截器无效,访问接口 /user/xxx/xxx 没有进入到拦截器中 @Configuration public class WebConfigurer implements WebMvcConfigurer { @Autowired private UserInterceptor userInterceptor; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(userInterceptor).addPathPatterns("/user/*"); } } }原因是 拦截器的匹配规则弄错了。 我希望 userInterceptor拦截器 拦截 /user/下所有路径的接口正确拦截规则是 /user/** 拦截规则粟子 addPathPatterns(“/“) 表示拦截所有的请求, addPathPatterns(”/”) 表示拦截所有的请求, addPathPatterns(“/test/“) 表示拦截/test/ 下的所有路径请求, addPathPatterns(”/test/*“) 表示拦截/test/abc,拦截/test/aaa , 不拦截 /test/abc/def addPathPatterns(”/test/”).excludePathPatterns(“/test/login”, “/test/register”) 表示拦截/test/ 下的所有路径请求,但不拦截 /test/login 和 /test/register |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |